<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ page import="com.CGFrame.struts.POJOs.Shot" %>
<%@ page import="com.CGFrame.struts.POJOs.Element" %>
<%@ page import="com.CGFrame.struts.POJOs.Elementgroup" %>
<%@ page import="com.CGFrame.struts.POJOs.Shotgroup" %>
<%@ page import="com.CGFrame.struts.POJOs.Catemodel" %>
<%@ page import="com.CGFrame.struts.bean.unit.ShotGroupBean" %>
<%@ page import="com.CGFrame.struts.bean.unit.ElementGroupBean" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Set Model </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<LINK href="../styles/sciencee.css"  type=text/css rel=stylesheet>
	<script language=javascript>
		function showImg(name){
			var a=document.getElementById("modelImg");
			a.setAttribute("src","<%=request.getContextPath() %>/categorySet/images/"+name+".jpg");
		}
	function create1(){
  		var radios = document.getElementsByName("rd");
  		var haveSelectedRadio=false;
		for(var i=0;i<radios.length;i++){
  			if(radios[i].checked==true){
  				var id=radios[i].getAttribute("id");
				setModel(radios[i].value,id);
				haveSelectedRadio=true;
			}
  		}
  		if(!haveSelectedRadio){
  			alert('<bean:message key="selectmodelplease"/>!');
  		}
	}
	function setModel(cmodel,id){
		var checks=document.getElementsByName("elementCheck");
		if(navigator.appName=="Microsoft Internet Explorer"){
			for(var i=0;i<checks.length;i++){
				if(checks[i].checked==true){
					checks[i].checked=false;
					var a=checks[i].parentNode.nextSibling.nextSibling;
					a.innerHTML=cmodel;
					a.setAttribute("id",id);
				}
			}
  		}else{
  			for(var i=0;i<checks.length;i++){
  				if(checks[i].checked==true){
  				  	checks[i].checked=false;
					var a=checks[i].parentNode.nextSibling.nextSibling.nextSibling.nextSibling;
					a.innerHTML=cmodel;
					a.setAttribute("id",id);
				}
			}
		}
		var checkAll=document.getElementById("selA");
		checkAll.checked=false;
	}
	function submitAll(){
		var elements=new Array();
		var models=new Array();
		var tds=document.getElementsByTagName("td");
		for(var i=0;i<tds.length;i++){
			if(tds[i].getAttribute("name")=="elementName"){
				elements.push(tds[i].getAttribute("id"));
			}
			if(tds[i].getAttribute("name")=="modelName"){
				models.push(tds[i].getAttribute("id"));
			}
		}
		var ar=new Array();
		var elementResult="";
		var v="0";
		for (var j=0;j<elements.length;j++){
			if(models[j]!=null && models[j]!=""){
				var vArr=new Array(models[j],elements[j]);
				ar.push(vArr);
				ar.sort();
			}
		}
		for(var k=0;k<ar.length;k++){
			if((ar[k])[0]!=v){
				v=(ar[k])[0];
				elementResult+=";"+(ar[k])[0]+":"+(ar[k])[1];
			}else{
				elementResult+=","+(ar[k])[1];
			}
		}
		var eModel=document.getElementById("eModel");
		eModel.value=elementResult;
		var form1=document.getElementById("form1");
		form1.submit();
	}
		function selectAll(){
			var check=document.getElementById("selA");
			var checks=document.getElementsByName("elementCheck");
			for(var i=0;i<checks.length;i++){
				if(checks[i].parentNode.parentNode.style.display==""){
					checks[i].checked=check.checked;
				}
			}
		}
		function undo1(){
			var checks=document.getElementsByName("elementCheck");
			if(navigator.appName=="Microsoft Internet Explorer"){
				for(var i=0;i<checks.length;i++){
					if(checks[i].checked==true){
						checks[i].checked=false;
						var a=checks[i].parentNode.nextSibling.nextSibling;
						a.innerHTML="";
						a.setAttribute("id","");
					}
				}
  			}else{
  				for(var i=0;i<checks.length;i++){
  					if(checks[i].checked==true){
  						checks[i].checked=false;
						var a=checks[i].parentNode.nextSibling.nextSibling.nextSibling.nextSibling;
						a.innerHTML="";
						a.setAttribute("id","");
					}
				}
			}
		}
		function changeElement(){
			var trs=document.getElementsByTagName("tr");
			for(j=0;j<trs.length;j++){
				if(trs[j].getAttribute("name")=="elementInfo"){
					trs[j].style.display="none";
				}
			}
			var sel=document.getElementById("eSel");
			if(sel.value=="0"){
				var vids="";
				var options=sel.options;
				for(var i=0;i<options.length;i++){
					vids+=options[i].getAttribute("value")+",";
				}
				var ids=vids.split(",");
				showShotRow(ids);
			}else{
				var ids=sel.value.split(",");
				showShotRow(ids);
			}
		}
		function showShotRow(ids){
			for(var i=0;i<ids.length;i++){
				if(ids[i]!=null && ids[i]!="" && ids[i]!="0"){
					var td=document.getElementById(ids[i]);
					td.parentNode.style.display="";
				}
			}
		}
	</script>
	<LINK href="styles/stylewang.css"  type=text/css rel=stylesheet>
    <style type="text/css">
<!--
.STYLE1 {
	font-family: "宋体";
	font-size: 12px;
}
-->
    </style>
</head>
  
  <body >
    <form id="form1" action="<%=path %>/project/relateElementModel.do" method=POST>
    <center>
      <span class="STYLE1"><bean:message key="setmodelforelement"/></span>
    </center>
    <input type="hidden" name="eles" id="eModel">
    <input type="hidden" name="projectId" value="<%=request.getAttribute("projectId") %>">
    <table><tr><td>
    	<select id="eSel" onChange="changeElement();">
    		<option value="0"><bean:message key="selectall"/></option>
    		<%
    			Set eSet=(Set)request.getAttribute("elementGroupList");
    			for(Iterator iter=eSet.iterator();iter.hasNext();){
    				Elementgroup eg=(Elementgroup)iter.next();
    				ElementGroupBean egb=new ElementGroupBean(eg);
    		 %>
    		 <option value="<%=egb.getAllElementsId()%>"><%=eg.getElementGroupName() %></option>
    		 <%} %>
    	</select>
    </td></tr></table>
    <table><tr><td valign="top">
    <table border="0" cellpadding="0" cellspacing="0" width="250">
        <tr>
    		<td class="btleftbian"><input id="selA" type="checkbox" onClick="selectAll();"></td>
    		<td class="btcenterbian"><bean:message key="selectall"/></td>
    		<td class="btcenterbian"><bean:message key="element.name"/></td>
    		<td class="btrightbian"><bean:message key="modelselected"/></td>
    	</tr>
    	<%
    		LinkedHashSet list=(LinkedHashSet)request.getAttribute("elementList");
    		for(Iterator iter=list.iterator();iter.hasNext();){
    			Element element=(Element)iter.next();
    	%>
    	<tr name="elementInfo">
    		<td colspan="2" class="leftcenter"><input type="checkbox" name="elementCheck" value="<%=element.getElementId() %>"></td>
    		<td class="leftcenter" id="<%=element.getElementId() %>" name="elementName"><%=element.getElementName() %></td>
    		<td name="modelName" class="rightcenter">&nbsp;</td>
    	</tr>
    	<%} %>
		<tr><td id="tdtop" colspan="4">&nbsp;</td></tr>
    </table>
    </td>
    <td valign="top">
    <table border="0" cellspacing="0" cellpadding="0">
    <tr><td>
    	<table border="0" cellpadding="0" cellspacing="0" width="260">
    	<tr>
    		<td class="btleftbian"><bean:message key="model"/></td>
    		<td class="btcenterbian"><bean:message key="modelname"/></td>
    		<td class="btcenterbian"><bean:message key="modeltype"/></td>
    		<td class="btrightbian"><bean:message key="modeldes"/></td>
    	</tr>
    	<%
    		List models=(List)request.getAttribute("elementModels");
    		for(Iterator iter=models.iterator();iter.hasNext();){
    			Catemodel shotModel=(Catemodel)iter.next();
    	%>
    		<tr>
    			<td class="leftcenter">
    			<input id="<%=shotModel.getCateModelId() %>" type="radio" name="rd" value="<%=shotModel.getCateModelName() %>" onClick="showImg('<%=shotModel.getCateModelName() %>');">
    			</td>
    			<td class="leftcenter"><%=shotModel.getCateModelName() %></td>
    			<td class="leftcenter"><%=shotModel.getCateModelType() %></td>
    			<td class="rightcenter"><%=shotModel.getDescription() %></td>
    		</tr>
    	<%} %>
		<tr><td id="tdtop" colspan="4">&nbsp;</td></tr>
    	<tr><td ><input type="button" value="Create" onClick="create1();"></td>
    		<td><input type="button" value="Undo" onClick="undo1();"></td>
    	</tr>
    	</table>
    </td>
    <td><img id="modelImg" alt='<bean:message key="previewpictureofdirectory"/>'/></td>
    </tr>
    </table>
    </td>
    </tr></table>
    <input type="button" onClick="submitAll();" value="<bean:message key='submit'/>">
    <input type="reset" value="<bean:message key='reset'/>">
    <input type="button" value="<bean:message key='back'/>" onClick="history.back();">
    </form>
  </body>
</html>

